package com.hefu.virtuallife.utils;


import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Arrays;

/**
* creat_user: zhengzaihong
* Email:1096877329@qq.com
* creat_date: 2018/4/18
* creat_time: 19:06
* describe Gson 小工具
**/

public class GsonUtil {

    private GsonUtil(){}

    private static  GsonUtil gsonUtil;
    public static GsonUtil get(){
        if(null==gsonUtil){
            synchronized (GsonUtil.class){
                gsonUtil = new GsonUtil();
            }
        }
        return gsonUtil;
    }

    /**
     * @param tags 需要被Gosn忽略映射的字段
     * @return
     */
    public Gson getIgnoreTag(final String...tags){
        Gson gson = new GsonBuilder().setExclusionStrategies(
                new ExclusionStrategy() {
                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                        return Arrays.asList(tags).contains(f.getName());
                    }
                    @Override
                    public boolean shouldSkipClass(Class<?> clazz) {
                        // 过滤掉 类名包含 Bean的类
                        return false;
                    }
                }).create();

        return gson;
    }
}
